package ast;

import java.util.ArrayList;

public class Evaluation {
    
    private ArrayList<StringBuffer> buffers;
    private int currentLine;
    private ArrayList<String> toPrint, toDebug, nfas, dfas;
    private String input;
    private boolean showDFA = false, showNFA = false, showDebug = false;
    
    public Evaluation(String input)
    {
        this.input = input;
        buffers = new ArrayList<StringBuffer>();
        for (String s : input.split("\\n"))
            buffers.add(new StringBuffer(s));
        toPrint = new ArrayList<String>();
        toDebug = new ArrayList<String>();
        nfas = new ArrayList<String>();
        dfas = new ArrayList<String>();
        currentLine = 0;
        toDebug.add(buffers.get(currentLine).toString());
    }
    
    public boolean isShowDFA() {
		return showDFA;
	}

	public void setShowDFA(boolean showDFA) {
		this.showDFA = showDFA;
	}

	public boolean isShowNFA() {
		return showNFA;
	}

	public void setShowNFA(boolean showNFA) {
		this.showNFA = showNFA;
	}

	public boolean isShowDebug() {
		return showDebug;
	}

	public void setShowDebug(boolean showDebug) {
		this.showDebug = showDebug;
	}

	public int getCurrentLineNumber()
    {
        return currentLine;
    }
    
    public String getInput()
    {
        return input;
    }
    
    public void addNFA(String s)
    {
    	nfas.add(s);
    }
    
    public void addDFA(String s)
    {
    	dfas.add(s);
    }
    
    public String getOutput()
    {
        StringBuffer acc = new StringBuffer();
        
        if(isShowDFA())
        {
        	for(String d : dfas)
        		acc.append(d + "\n");
        	acc.append("\n");
        }

        if(isShowNFA())
        {
        	for(String n : nfas)
        		acc.append(n + "\n");
        	acc.append("\n");
        }
        
        if(isShowDebug())
        	for(String line : toDebug)
                acc.append(line + "\n");
        else
        	for(String line : toPrint)
                acc.append(line + "\n");
        return acc.toString();
    }
    
    public String getLine(int i) throws ArrayIndexOutOfBoundsException
    {
        return buffers.get(i).toString();
    }

    public String getCurrentLine()
    {
        return getLine(currentLine);
    }
    
    public boolean nextLine()
    {
        if (currentLine + 1 < buffers.size())
        {
            currentLine ++;
            toDebug.add(getCurrentLine());
            return true;
        }
        return false;
    }
    
//    public boolean prevLine()
//    {
//        if (currentLine > 0)
//        {
//            currentLine --;
//            return true;
//        }
//        return false;
//    }
    
    public void print(String s)
    {
        toPrint.add(s);
    }
    
//    public boolean gotoLine(int i) 
//    {
//        if(i >= 0 && i < buffers.size())
//        {
//            currentLine = i;
//            return true;
//        }
//        return false;
//    }
    
    public void remove(char a)
    {
        int index = 0;
        StringBuffer b = buffers.get(currentLine);
        while((index = b.indexOf(a + "")) != -1)
            b.deleteCharAt(index);
        toDebug.add("remove : " + getCurrentLine());
    }
    
    public void insert(int index, String s)
    {
    	StringBuffer buf = buffers.get(currentLine); 
    	if(index > buf.length())
    		buf.append(s);
    	else
    		buf.insert(index, s);
    	toDebug.add("insert : " + getCurrentLine());
    }
    
    public void replace(char a, char b)
    {
        int index = 0;
        StringBuffer buf = buffers.get(currentLine);
        while((index = buf.indexOf(a + "")) != -1)
            buf.setCharAt(index, b);
        toDebug.add("replace : " + getCurrentLine());
    }
    
    public int length()
    {
        return getCurrentBuffer().length();
    }
    
    public void substring(int i, int j)
    {
        StringBuffer buf = getCurrentBuffer();
        buf.delete(0, i);
        if(j < buf.length())
            buf.delete(j, buf.length());
        toDebug.add("substring : " + getCurrentLine());
    }
    
    private StringBuffer getCurrentBuffer()
    {
        return buffers.get(currentLine);
    }
}